﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XW.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public static partial class CommonHelper
    {
        /// <summary>
        /// 四色五入方法
        /// 如果结果为0 则小数位自动+1， 直到最大小数位
        /// </summary>
        /// <param name="d"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static decimal Round(decimal d, int minLength =0, int maxLength=6) {
           
            if (minLength >= maxLength) {
                return decimal.Round(d, minLength, MidpointRounding.AwayFromZero);
            }
            decimal r = 0;
            for (int i = minLength; i <= maxLength; i++) {
                r = decimal.Round(d, i, MidpointRounding.AwayFromZero);
                if (r>0)
                {
                    return r;
                } 
            }
            return r;
        }
    }
}
